package com.yutel.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import cn.com.xinli.mobile.packet.AbstractPacket;
import cn.com.xinli.mobile.packet.Protocol;

public class ClientObject {
	private static ClientObject cs;
	private Socket client;
	private String ip;
	private int port;
	private int timeout;

	private ClientObject() {
	}

	public static ClientObject getInstance() {
		if (cs == null) {
			cs = new ClientObject();
		}
		return cs;
	}

	public void send(AbstractPacket packet) {
		ObjectOutputStream out = null;
		try {
			client = new Socket(ip, port);
			out = new ObjectOutputStream(client.getOutputStream());
			out.writeObject(packet);
			ObjectInputStream in =new ObjectInputStream(client.getInputStream());
			Object o=in.readObject();
			System.out.println(o.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void stop() {
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public static void main(String[] args) {
		ClientObject c = ClientObject.getInstance();
		c.setIp("192.168.4.27");
		c.setPort(9080);
		c.setTimeout(40000);
		AbstractPacket ap = new AbstractPacket(Protocol.Command.content);
		ap.setValue("http://www.iqiyi.com/dianying/20120309/46c750b00bbdaba9.html");
//		ap.setValue("www.iqiyi.com/yinyue/20110104/1cbec011b4cae654.html22444111234");
//		ap.setValue("http://www.iqiyi.com/yinyue/20110104/1cbec011b4cae654.html");
		c.send(ap);
		c.stop();
	}
}
